<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of StudentController
 *
 * @author gc
 */
class StudentController extends AdminController
{

    public function index()
    {
        $name = I('name');
        $map['status'] = array('egt', 0);
        if (!empty($name)) {
            $map['name'] = array('like', '%' . (string) $name . '%');
        }
        
        $status = I("status");
        if (!empty($status)) {
            $map['status'] = $status;
        }

        $list = $this->lists('Student', $map, 'create_time desc');
        $this->assign('_list', $list);

        $this->meta_title = '学生列表';
        $this->showAttr();
        $this->display();
    }

    private function showAttr()
    {
        $this->assign('_source', [1 => '自然', 2 => '介绍']);
        $this->assign('_status', [1 => '待确认', 2 => '已确认', 3 => '已签约']);
    }

    public function add()
    {
        $id = I("id");
        $meta_title = "新增学生";
        if (!empty($id)) {
            $map["id"] = $id;
            $data = M("Student")->where($map)->find();
            $data['birthday'] = time_format($data['birthday'], 'Y-m-d');
            $this->assign("data", $data);
            $meta_title = "编辑学生";
        }
        $this->meta_title = $meta_title;
        $this->showAttr();
        $this->display();
    }

    public function save()
    {
        $data = I("post.");

        $id = I("id");
        $data['birthday'] = strtotime($data['birthday']);
        if (empty($id)) {
            if (!isset($data['source'])) {
                $data['source'] = 1;
            }
            $data['create_time'] = $data['update_time'] = time();
            unset($data['id']);
        } else {
            $data['update_time'] = time();
        }

        $model = M("Student");
        $s = empty($id) ? $model->add($data) : $model->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

    public function delete()
    {
        $ids = I("id");
        $map['id'] = array('in', $ids);

        $data = [
            'status' => -1,
            'update_time' => time()
        ];

        $s = M("Student")->where($map)->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

}
